<%--
  Created by IntelliJ IDEA.
  User: zhangDongFei
  Date: 2020/11/23
  Time: 16:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>客户显示</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<%--模糊查询  输入框--%>
<div class="layui-elem-field">
    <form class="layui-form layui-form-pane" id="searchNewsFrom">
        <div class="layui-row">
            <div class="layui-inline layui-col-md-offset1">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" class="layui-input" placeholder="请输入姓名">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="text" name="iphone" class="layui-input" placeholder="请输入手机号">
                </div>
                <button type="button" data-type="reload"
                        class="layui-btn layui-btn-radius layui-btn-normal layui-icon layui-icon-search"
                        lay-submit lay-filter="searchBtn">查询
                </button>
            </div>
        </div>
    </form>
</div>


<div id="updGustForm">
    <form class="layui-form layui-from-pane" action="" lay-filter="modify">
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-inline">
                <input type="text" name="id" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名字</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">身份证号</label>
            <div class="layui-input-inline">
                <input type="text" name="idcard" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name="status">
                    <option value="0" selected>正常</option>
                    <option value="1">禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo1" id="formDemo1">立即提交</button>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script>

    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;

        var condition="";
        $("#updGustForm").hide();
        $(function () {
                showTable();
        })
        function showTable(){
            table.render({
                elem: '#test' //id为test的table
                , page: true
                , limit: 5
                , limits: [5, 10, 15]
                , request: {
                    pageName: "pa"
                    , limitName: "li"
                },
                where:{condition:condition}
                , url: '${pageContext.request.contextPath}/cou'   //url 查询数据   所有的客户的数据
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '客户数据表'
                //data[{ id:12,name:123}]
                , cols: [[
                    {field: 'id', title: 'ID', fixed: 'left', sort: true}
                    , {field: 'name', title: '客户名'}
                    , {field: 'iphone', title: '手机号', sort: true}
                    , {field: 'idcard', title: '身份证号', sort: true}
                    , {
                        field: 'status', title: '状态', sort: true, templet: function (res) {
                            //res  这一行的内容
                            var f = res.status;//状态值
                            if (f == 0) {
                                //正常
                                return "正常";
                            } else {
                                return "禁用";
                            }

                        }
                    }
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
                ]]
            });
        }




        //查询的提交事件
        form.on('submit(searchBtn)', function (data) {
            condition="";
            // alert("name1"+data.field.name);
            // alert("ipone2"+data.field.iphone);
            if(data.field.name!=null && data.field.name!=="" ){
                condition += " and name like '%"+data.field.name+"%' ";
            }
            if(data.field.iphone!=null && data.field.iphone!==""){
                condition += " and iphone like '%"+data.field.iphone+"%' ";
            }
            showTable()
            return false;
        });





        //监听行工具事件  test  表格的id
        table.on('tool(test)', function (obj) {
            //obj 代表 一行的内容
            var data = obj.data; //一行的数据
            //console.log(obj)
            /*  if(obj.event === 'del'){
                  layer.confirm('真的删除行么', function(index){
                      // obj.del();//页面删除
                      window.location.href="
           /gust?method=del&gid="+data.id;
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){*/
            form.val("modify", data);
            layer.open({
                title: '修改信息'
                , type: 1
                , offset: '100px'
                , content: $("#updGustForm")
            });

            form.on('submit(formDemo1)', function (data) {
                //layer.msg(JSON.stringify(data.field));
                //1.将获取到到的数据保存到数据库
                $.ajax({
                    url: "${pageContext.request.contextPath}/gai",
                    // dataType:"json",
                    data: data.field,  //当前表单里面填写的数据{name:'',name1:''}
                    type: "post",
                    success: function (r) {
                        alert(r);
                        if (r>0) {
                            layer.closeAll();
                            //刷新一下table
                            table.reload('test', {
                                page: {
                                    curr: 1
                                }
                            })
                        }
                    }, error: function () {
                        alert("服务器错误");
                    }
                })
                return false;
            });

            /* layer.prompt({
                 formType: 2
                 ,value: data.email
             }, function(value, index){
                 obj.update({
                     email: value
                 });
                 layer.close(index);
             });*/

        });


    });
</script>

</body>
</html>
